
class MyDate extends Date {
	// 时间格式化
	timeFormat(format = "yyyy-mm-dd hh:MM:ss") {
		return uni.$u.timeFormat(this.valueOf(), format)
	}
	// 新的时间格式化
	newTimeFormat(format='YYYY-MM-DD HH:mm:ss'){
		let result = format
		.replace('YYYY',this.getFullYear())
		.replace('MM',this.getMonth()+1)
		.replace('DD',this.getDate())
		.replace('HH',this.getHours())
		.replace('mm',this.getMinutes())
		.replace('ss',this.getSeconds())
		return result
	}
	// 增大时间
	addTime(num, type) {
		if (['y', 'years'].includes(type)) {
			this.setFullYear(this.getFullYear() + num)
		} else if (['months', 'M'].includes(type)) {
			this.setMonth(this.getMonth() + num)
		} else if (['days', 'd'].includes(type)) {
			this.setDate(this.getDate() + num)
		} else if (['hours', 'h'].includes(type)) {
			this.setHours(this.getHours() + num)
		} else if (['minutes', 'm'].includes(type)) {
			this.setMinutes(this.getMinutes() + num)
		} else if (['seconds', 's'].includes(type)) {
			this.setSeconds(this.getSeconds() + num)
		}
		return this
	}
	// 减小时间
	subtractTime(num, type) {
		if (['y', 'years'].includes(type)) {
			this.setFullYear(this.getFullYear()-num)
		} else if (['months', 'M'].includes(type)) {
			let currM = this.getMonth()
			let newM = currM - num
			if(newM>=0){
				this.setMonth(newM)
			}else{
				let bigM = num-currM
				this.setFullYear(this.getFullYear()-Math.floor(bigM/12))
				this.setMonth(12-bigM%12)
			}
		} else if (['days', 'd'].includes(type)) {
			this.setTime(this.getTime()-num*24*60*60*1000)
		} else if (['hours', 'h'].includes(type)) {
			this.setTime(this.getTime()-num*60*60*1000)
		} else if (['minutes', 'm'].includes(type)) {
			this.setTime(this.getTime()-num*60*1000)
		} else if (['seconds', 's'].includes(type)) {
			this.setTime(this.getTime()-num*1000)
		}
		return this
	}
	// this<target
	isBefore(target,type) {
		// let tar =  new MyDate(target)
		// if(!type){
		// 	return this.valueOf() < tar.valueOf()
		// }else if(type==='year'){
		// 	return this.getFullYear()===tar.getFullTear()
		// }else if(type==='month'){
		// 	return this.isSame this.getMonth()===tar.getMonth()
		// }else if(type==='day'){
		// 	return this.getMonth()===tar.getMonth()
		// }else if(type==='hour'){
		// 	return this.getMonth()===tar.getMonth()
		// }else if(type==='minute'){
		// 	return this.getMonth()===tar.getMonth()
		// }else if(type==='second'){
		// 	return this.getMonth()===tar.getMonth()
		// }
		return this.valueOf() < new MyDate(target).valueOf()
	}
	// this===target
	isSame(target) {
		return this.valueOf() === new MyDate(target).valueOf()
	}
	// this>target
	isAfter(target){
		return this.valueOf() > new MyDate(target).valueOf()
	}
	// this<=target
	isSameOrBefore(target) {
		return this.valueOf() <= new MyDate(target).valueOf()
	}
	// this>=target
	isSameOrAfter(target){
		return this.valueOf() >= new MyDate(target).valueOf()
	}

}
// 创建date对象
function myDate(time) {
	return [null,undefined].includes(time)?(new MyDate()):(new MyDate(time))
}

// 获取时间戳
function valueOfTime(time) {
	return myDate(time).getTime()
}

// 时间、时间戳->格式化
function timeFormat(time, format = "yyyy-mm-dd hh:MM:ss") {
	return myDate(time).timeFormat(format)
}
const timeLengthMap = {
	Y:60*60*24*30*12,
	M:60*60*24*30,//暂定一个月有30天
	D:60*60*24,
	H:60*60,
	m:60,
	s:1,
}

function getTimeLength(key='m',unit='ms'){ //unit:s秒,ms毫秒
	return unit==='ms'?timeLengthMap[key]*1000:timeLengthMap[key]
}

// 剩余时间
function timeRemaining(timeLength=0,format='H小时m分钟'){
	// 剩余时间不允许大于0
	if(!timeLength||timeLength<0){
		timeLength = 0
	}
	if(!format){
		format = 'H小时m分钟'
	}
	let result = format
	if(result.includes('Y')){
		let Y = Math.floor(timeLength/getTimeLength('Y'))
		result = result.replace('Y',`${Y}`.padStart(2,0))
		timeLength = timeLength - Y*getTimeLength('Y')
	}
	if(result.includes('M')){
		let M = Math.floor(timeLength/getTimeLength('M'))
		result = result.replace('M', `${M}`.padStart(2,0) )
		timeLength = timeLength - M*getTimeLength('M')
	}
	if(result.includes('D')){
		let D = Math.floor(timeLength/getTimeLength('D'))
		result = result.replace('D', `${D}`.padStart(2,0) )
		timeLength = timeLength - D*getTimeLength('D')
	}
	if(result.includes('H')){
		let H = Math.floor(timeLength/getTimeLength('H'))
		result = result.replace('H', `${H}`.padStart(2,0) )
		timeLength = timeLength - H*getTimeLength('H')
	}
	if(result.includes('m')){
		let m = Math.floor(timeLength/getTimeLength('m'))
		result = result.replace('m', `${m}`.padStart(2,0) )
		timeLength = timeLength - m*getTimeLength('m')
	}
	if(result.includes('s')){
		let s = Math.floor(timeLength/getTimeLength('s'))
		result = result.replace('s', `${s}`.padStart(2,0) )
		timeLength = timeLength - s*getTimeLength('s')
	}
	return result
}

// 比较差值:enableNegative是否允许负数
function diffValue(newTime,oldTime,enableNegative=false){
	let result = myDate(newTime).valueOf() - myDate(oldTime).valueOf()
	if((!result||result<0)&&!enableNegative){
		result = 0
	}
	return result
}

export default {
	myDate,
	valueOfTime,
	timeFormat,
	getTimeLength,
	timeRemaining,
	diffValue,
}
